#include"process.h"
#include<unistd.h>
#include<string.h>

#define NUM 101
#define STYLE '#'

void FlushProcess(double total, double current)
{

    char buffer[NUM];
    memset(buffer, 0, sizeof(buffer));
    const char* label = "|/-\\";
    int len = strlen(label);
    
    static int cnt = 0; // 记录label即使进度条停止，但label仍在变化，说明仅仅可能是网络问题导致下载啊速度慢导致，而并不是下载停止了。

    double rate = current / total * 100;
    int n = (int)rate;
    int i = 0;
    for(i = 0; i < n; ++i)
    {
        buffer[i] = STYLE;
    }

    cnt %= len;
    printf("[%-100s][%.1f%%][%c]\r",buffer, rate, label[cnt]);
    cnt++;
    fflush(stdout);
}

void process_v1()
{
    char buffer[NUM];
    memset(buffer, 0, sizeof(buffer));
    int cnt = 0;
    const char* label = "|/-\\";
    int len = strlen(label);
    
    while(cnt < NUM)
    {
        printf("[%-100s] [%d%%] [%c]\r",buffer,cnt,label[cnt%len]);
        fflush(stdout);
        buffer[cnt] = STYLE;
        cnt++;
        usleep(50000);
    }
    printf("\n");
}
